package com.gange.service.impl;

import com.gange.aspect.annotation.Log;
import com.gange.aspect.annotation.LogAnnotation;
import com.gange.mapper.DeptMapper;
import com.gange.mapper.EmpMapper;
import com.gange.pojo.Dept;
import com.gange.pojo.Emp;
import com.gange.service.DeptService;
import com.gange.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper dm;
    @Autowired
    private EmpMapper em;
    @LogAnnotation
    public List<Dept> findAll(){
        return dm.findAll();
    }
    @Log
    @Override
    public void add(Dept dept) {
        //补全部门数据
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        //调用持久层增加功能
        dm.inser(dept);
    }

    @Autowired
    private EmpService es;
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    @Override
    @Log
    public void delete(int id) throws ClassNotFoundException {
       try {
           dm.delete(id);
           int a = 1 / 0;
           em.deleteByDid(id);

       }catch (Exception e){
           throw e;
       }finally {
           Emp emp = new Emp();
           emp.setName("222");
           emp.setDeptId(2);
           emp.setUsername("222");
           emp.setPassword("123");
           emp.setGender(Short.parseShort("1"));
           es.save(emp);
       }

    }
}
